﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

using ScytheEngine;

using VTESCore;

namespace VTESGameLogicWindows.GameStates
{
	public class MultiPlayerGame : GameplayScreen
	{
		Texture2D m_CurrentCardTexture;
		VTESDeck m_FNCards;
		Dictionary<VTESCard, Texture2D> m_CardToTextureDictionary = new Dictionary<VTESCard, Texture2D>();

		#region Initialization

		protected override void LoadCustomStateContent()
		{
			ContentManager decks_content = new ContentManager( GameStateManager.Game.Services, "Content\\decks" );

			m_FNCards = decks_content.Load<VTESDeck>( "FN" );

			foreach ( VTESCard card in m_FNCards )
			{
				ContentManager card_images_content = new ContentManager( GameStateManager.Game.Services, "Content\\cardimages" + Path.GetDirectoryName( card.CardArtPath ) );
				Texture2D card_texture = card_images_content.Load<Texture2D>( Path.GetFileNameWithoutExtension( card.CardArtPath ) );
				m_CardToTextureDictionary.Add( card, card_texture );
			}

			m_CurrentCardTexture = m_CardToTextureDictionary[m_FNCards.CryptCards[0]];
		}

		#endregion

		#region Update and Draw

		/// <summary>
		/// Updates the state of the game.
		/// </summary>
		public override void Update( GameTime gameTime, bool otherScreenHasFocus,
													   bool coveredByOtherScreen )
		{
			base.Update( gameTime, otherScreenHasFocus, coveredByOtherScreen );
		}


		/// <summary>
		/// Lets the game respond to player input. Unlike the Update method,
		/// this will only be called when the gameplay screen is active.
		/// </summary>
		public override void HandleInput( InputState input )
		{
			if ( input == null )
				throw new ArgumentNullException( "input" );

			if ( input.PauseGame )
			{
				// If they pressed pause, bring up the pause menu screen.
				GameStateManager.AddState( GameStateManager.GetSpecialScreen( ScreenType.GamePausedScreen ) );
			}

		}


		/// <summary>
		/// Draws the gameplay screen.
		/// </summary>
		public override void Draw( GameTime gameTime )
		{
			// This game has a blue background. Why? Because!
			GameStateManager.Game.GraphicsDevice.Clear( ClearOptions.Target,
											   Color.CornflowerBlue, 0, 0 );

			SpriteBatch spriteBatch = GameStateManager.SpriteBatch;
			Viewport viewport = GameStateManager.Game.GraphicsDevice.Viewport;
			Vector2 card_pos = new Vector2( viewport.Width / 2 - m_CurrentCardTexture.Width / 2, viewport.Height / 2 - m_CurrentCardTexture.Height / 2 );
			Rectangle card_rect = new Rectangle( (int)card_pos.X, (int)card_pos.Y, m_CurrentCardTexture.Width, m_CurrentCardTexture.Height );

			spriteBatch.Begin( SpriteBlendMode.None );

			spriteBatch.Draw( m_CurrentCardTexture, card_rect, Color.White );

			spriteBatch.End();

			// If the game is transitioning on or off, fade it out to black.
			if ( TransitionPosition > 0 )
				GameStateManager.FadeBackBufferToBlack( 255 - TransitionAlpha );
		}

		#endregion
	}

}
